// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   ConfigParser.java

package ys8583.parse;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import ys8583.po.Field;
import ys8583.po.MessageConf;

// Referenced classes of package ys8583.parse:
//			Conf8583

public class ConfigParser {

	public ConfigParser() {}

//	public static void main(String args[]) {
//		ConfigParser c = new ConfigParser();
//		try {
//			c.parse((new StringBuilder("conf")).append(File.separatorChar)
//					.append("parse.xml").toString(),
//					(new StringBuilder("conf")).append(File.separatorChar)
//							.append("log4j.xml").toString());
//			MessageConf mesConf = MessageConf.getInstance();
//			Map map = mesConf.getMap();
//			Conf8583 cups = (Conf8583) map.get("CUPS");
//			System.out.println(cups.getBitmap());
//			System.out.println(cups.getItem());
//			Map templateMap = cups.getTemplateMap();
//			Map headerMap = cups.getHeaderMap();
//			Field f3 = (Field) templateMap.get("3");
//			Field h1 = (Field) headerMap.get("1");
//			System.out.println(f3.getAlias());
//			System.out.println(h1.getAlias());
//			for (int i = 0; i < ((List) cups.getMessageMap().get("0810"))
//					.size(); i++)
//				System.out.println(((List) cups.getMessageMap().get("0810"))
//						.get(i));
//
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

	public void parse(File file) throws Exception {
		DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();
		DocumentBuilder docb = null;
		Document doc = null;
		docb = docfact.newDocumentBuilder();
		try {
			docb = docfact.newDocumentBuilder();
			doc = docb.parse(file);
		} catch (Exception ex) {
			throw ex;
		}
		Element root = doc.getDocumentElement();
		MessageConf mesConf = MessageConf.getInstance();
		Map confMap = new HashMap();
		Conf8583 cf = null;
		Field f = null;
		Map headerMap = new HashMap();
		Map templateMap = new HashMap();
		Map messageMap = new HashMap();
		NodeList nodes = null;
		Element elem = null;
		nodes = root.getElementsByTagName("parse");
		for (int i = 0; i < nodes.getLength(); i++) {
			elem = (Element) nodes.item(i);
			cf = new Conf8583();
			cf.setItem(elem.getAttribute("item"));
			cf.setBitmap(elem.getAttribute("bitmap"));
			cf.setLenZeroFillType(elem.getAttribute("lenZeroFillType"));
			NodeList header = elem.getElementsByTagName("header");
			for (int j = 0; j < header.getLength(); j++) {
				Element eleHeader = (Element) header.item(j);
				cf.setHeaderLen(Integer.parseInt(eleHeader
						.getAttribute("length")));
				NodeList nodesHeader = eleHeader.getElementsByTagName("field");
				for (int z = 0; z < nodesHeader.getLength(); z++) {
					Element elemHeader = (Element) nodesHeader.item(z);
					f = new Field();
					f.setNum(elemHeader.getAttribute("num"));
					f.setType(elemHeader.getAttribute("type"));
					f.setFormat(elemHeader.getAttribute("format"));
					f.setLength(elemHeader.getAttribute("length"));
					f.setAlias(elemHeader.getAttribute("alias"));
					f.setLenType(elemHeader.getAttribute("lenType"));
					f.setFieldZeroFillType(elemHeader
							.getAttribute("fieldZeroFillType"));
					f.setIsTotalLen(elemHeader.getAttribute("isTotalLen"));
					headerMap.put(f.getNum(), f);
				}

			}

			NodeList nodesTemplate = elem.getElementsByTagName("template");
			for (int j = 0; j < nodesTemplate.getLength(); j++) {
				Element elemTemplate = (Element) nodesTemplate.item(j);
				NodeList nodesTemplateField = elemTemplate
						.getElementsByTagName("field");
				for (int z = 0; z < nodesTemplateField.getLength(); z++) {
					Element elemTemplateField = (Element) nodesTemplateField
							.item(z);
					f = new Field();
					f.setNum(elemTemplateField.getAttribute("num"));
					f.setType(elemTemplateField.getAttribute("type"));
					f.setFormat(elemTemplateField.getAttribute("format"));
					f.setLength(elemTemplateField.getAttribute("length"));
					f.setAlias(elemTemplateField.getAttribute("alias"));
					f.setLenType(elemTemplateField.getAttribute("lenType"));
					f.setFieldZeroFillType(elemTemplateField
							.getAttribute("fieldZeroFillType"));
					templateMap.put(f.getNum(), f);
				}

			}

			List mesList = null;
			NodeList nodesMessage = elem.getElementsByTagName("message");
			for (int j = 0; j < nodesMessage.getLength(); j++) {
				Element elemMessage = (Element) nodesMessage.item(j);
				String mesType = elemMessage.getAttribute("type");
				System.out.println((new StringBuilder(" message type = "))
						.append(mesType).toString());
				mesList = new ArrayList();
				NodeList nodesMessageField = elemMessage
						.getElementsByTagName("field");
				for (int z = 0; z < nodesMessageField.getLength(); z++) {
					Element elemMessageField = (Element) nodesMessageField
							.item(z);
					mesList.add(Integer.valueOf(elemMessageField
							.getAttribute("num")));
				}

				Collections.sort(mesList);
				messageMap.put(mesType, mesList);
			}

			cf.setHeaderMap(headerMap);
			cf.setTemplateMap(templateMap);
			cf.setMessageMap(messageMap);
			confMap.put(cf.getItem(), cf);
		}

		mesConf.setMap(confMap);
	}

}
